"use strict"

// instance is Type ?
export function getType (obj) {
    return Object.prototype.toString.call(obj).slice(8, -1)
}
// what is type ?
export function typeOf (obj) {
    const type = typeof obj;

    if (type !== 'object') {
        return type
    }

    return getType(obj)
}

// 根据路径获取对象的值
export function getPathValue (data={}, path='', df='') {
    return path.split('.').reduce((sourcre, key) => {
        return ( sourcre[key] || df );
    }, data);
}

// 字符串格式化
export function formatString (char, sourceData={}) {
    console.log(char, sourceData.test, 'func--')
    return char && char.replace && char.replace(/\{(.*?)\}/g,(_, index) => {
        return getPathValue(sourceData, index);
    });
}

// 创建一个函数的拷贝
// function createFunc (func) {
//     const [full, params, operator, funcBody] = func.toString().match(/\((.*?)\)([ ]|=>)*?\{(.*)?\}/m)
//     return new Function(...params.split(/,[ ]*/gm), funcBody)
// }

// 深拷贝执行函数 
export function deepCopy (obj, hash=new WeakMap()) {
    const type = getType(obj);
    if (type === typeof obj || type === 'Null') return obj;
    if (type === 'Object' || type == 'Array') {
        const allDesc = Object.getOwnPropertyDescriptors(obj);
        const cloneObj = Object.create(Object.getPrototypeOf(obj), allDesc);
        hash.set(obj, cloneObj);

        for (let key of Reflect.ownKeys(obj)) {
            cloneObj[key] = deepCopy(obj[key]);
        }

        return cloneObj
    }

    if (type === 'Date') return new Date(obj)
    if (type === 'RegExp') return new RegExp(obj)
}